//
//  OrderDetailViewController.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/3/4.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "CustomOrderDetailTopableViewCell.h"
#import "CustomOrderProductTableViewCell.h"
#import "CustomOrderPayTableViewCell.h"
#import "OrderDetailModel.h"
@interface OrderDetailViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    
}

@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"订单详情"];
    [self setNaviLeftButton:@""];
    [self loadOrderData];
    // Do any additional setup after loading the view.
}
-(void)loadOrderData{
     __weak typeof (self) weakSelf = self;
    [ApiClient getOrderDetailOrderID:self.orderID didFinishLoaded:^(OrderDetailModel *model, NSString *path) {
        
        _orderModel=model.data;
        
        if (![weakSelf.view.subviews containsObject:weakSelf.displayView]) {
            [weakSelf.view addSubview:weakSelf.displayView];
        }else{
            [weakSelf.displayView reloadData];
        }
       

        
    } didFailLoaded:^(NSError *error, NSString *path) {
        
    }];
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        // [_displayView setSeparatorColor:[UIColor redColor]];
    }
    return _displayView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _orderModel.products.count+2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        static NSString *identifier = @"CustomOrderDetailTopableViewCell";
        CustomOrderDetailTopableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                  identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderDetailTopableViewCell"  owner:self options:nil] lastObject];
            
        }
        [cell loadDataModel:_orderModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        return cell;
 
    }else if (indexPath.row==_orderModel.products.count+1){
        static NSString *identifier = @"CustomOrderPayTableViewCell";
        CustomOrderPayTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                             identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderPayTableViewCell"  owner:self options:nil] lastObject];
            
        }
        cell.payResultProcessBlock = ^(NSDictionary *resultDic) {
            NSInteger statusCode = [resultDic[@"resultStatus"] integerValue];
            if (statusCode == 9000) {
                [[LKDHud shareHud] showtip:@"支付成功"];
            } else {
                [[LKDHud shareHud] showtip:@"支付不成功"];
            }
            [self loadOrderData];
        };
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        return cell;

    }else{
        static NSString *identifier = @"CustomOrderProductTableViewCell";
        CustomOrderProductTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                 identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOrderProductTableViewCell"  owner:self options:nil] lastObject];
            
        }
        OrderProductModel *productModel=_orderModel.products[indexPath.row-1];
        [cell loadProductModel:productModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        return cell;

    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
         return 175;
    }else if (indexPath.row==_orderModel.products.count+1){
        return 182;
    }else{
        return 90;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
